<?php
namespace app\admin\model;
use think\Model;

class Goods extends Model
{

    protected static function init()
    {
        //添加之前的操作
        Goods::event('before_insert',function($goods){
          if($_FILES['pic']['tmp_name']){
                $file = request()->file('pic');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/index/image/picture');
                if($info){
                    $goods['pic']='/LOLforum/'.'public/static/'.'index/image/picture/'.date('Ymd',time()).'/'.$info->getFileName();
                }
            }
      });


        Goods::event('before_update',function($goods){
          if($_FILES['pic']['tmp_name']){
                $good=Goods::find($goods->id);
                $thumbpath=$_SERVER['DOCUMENT_ROOT'].$good['pic'];
                if(file_exists($thumbpath)){
                    @ungoods($thumbpath);
                }
                $file = request()->file('pic');
               $info = $file->move(ROOT_PATH . 'public' . DS . 'static/index/image/picture');
                if($info){
                    $goods['pic']='/LOLforum/'.'public/static/'.'index/image/picture/'.date('Ymd',time()).'/'.$info->getFileName();
                    db('goods')->where('id',$goods->data['id'])->update(['pic' => $goods['pic']]);
                }

            }
      });

        Goods::event('before_delete',function($goods){         
                $good=Goods::find($goods->id);
                $thumbpath=$_SERVER['DOCUMENT_ROOT'].$good['pic'];
                if(file_exists($thumbpath)){
                    @ungoods($thumbpath);
                }
        });

    }

}
